<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<%@ page import="za.org.foobar.famon.model.*" %>
<%@ page import="za.org.foobar.famon.model.StackTraceElement" %>
<%@ page import="za.org.foobar.famon.simpleui.PageUtil" %>
<%@ page import="za.org.foobar.famon.simpleui.SimpleUiUtil" %>
<%@ page import="java.util.List" %>
<%@ page import="za.org.foobar.famon.util.ServerUtil" %>
<%@ page import="za.org.foobar.famon.webapp.UserLogic" %>
<%@ page import="za.org.foobar.famon.webapp.FailureLogic" %>
<%@ page import="za.org.foobar.famon.webapp.FamonSecurityException" %>
<%
    Log log = LogFactory.getLog("za.org.foobar.famon.web.failure.jsp");
    FailureLogic failureLogic = FailureLogic.get();
    UserLogic userLogic = UserLogic.get();
    String paramFailureId = ServerUtil.urlDecode(request.getParameter("failureId"));
    String paramThreadId = ServerUtil.urlDecode(request.getParameter("threadId"));

    String baseUrl = SimpleUiUtil.getCurrentUrl(request).replaceAll("[\\&]threadId[\\=][\\d]+", "");

    PageUtil loggingEventPageUtil = new PageUtil(request, "le", "loggingEventList");
    PageUtil stackTracePageUtil = new PageUtil(request, "st", "stackTrace");
    
    StringBuilder error = new StringBuilder();
    
    Failure failure = null;
    if (paramFailureId == null) {
        SimpleUiUtil.addError(error, "No failure specified.");
    } else {
        try {
            failure = failureLogic.getFailure(Failure.stringToKey(paramFailureId));
            if (failure == null) {
                SimpleUiUtil.addError(error, "The selected failure could not be found");
            }
        } catch (FamonSecurityException ignored) {
            if (SimpleUiUtil.handleSecurityException(request, response, error)) {
                return;
            }
        }
    }
%>
<html>
<head>
    <title>Failure</title>
    <link rel="stylesheet" href="simpleui.css"/>
    <link rel="stylesheet" href="bluedream.css"/>
</head>
<body>
<%=SimpleUiUtil.renderLoginHeader(request)%>
<div class="breadCrumb">
    <a href="index.jsp">Failures</a>
    <%
    if (failure != null) {
        ApplicationGroup group = failure.getApplicationGroup();
        %> &gt; <a href="index.jsp?applicationGroupId=<%=group.keyToString()%>"><%=group.getTitle()%></a><%
        %> &gt; <a href="failure.jsp?failureId=<%=failure.keyToString()%>"><%=failure.getFailureType()%></a><%
    }
    %>
</div>
<div class="body">
<%
    if (error.length() > 0) {
        out.print(error.toString());
    } else if (failure != null) {
        %>
        <table width="100%">
            <caption>Failure</caption>
            <tbody>
                <tr class="odd">
                    <th width="20%">Type</th>
                    <td><%=failure.getFailureType()%></td>
                </tr><tr>
                    <th>Date</th>
                    <td><%=failure.getDate()%></td>
                </tr><tr class="odd">
                    <th>Message</th>
                    <td><%=failure.getMessage()%></td>
                </tr><tr>
                    <th>Source Hostname</th>
                    <td><%=failure.getSourceHostname()%></td>
                </tr><tr class="odd">
                    <th>Source Address</th>
                    <td><%=failure.getSourceAddress()%></td>
                </tr><tr>
                    <th>Source Port</th>
                    <td><%=failure.getSourcePort()%></td>
                </tr><tr class="odd">
                    <th>Original Source Hostname</th>
                    <td><%=failure.getOriginalSourceHostname()%></td>
                </tr><tr>
                    <th>Original Source Address</th>
                    <td><%=failure.getOriginalSourceAddress()%></td>
                </tr><tr class="odd">
                    <th>User</th>
                    <td><%=failure.getUser()%></td>
                </tr>
            </tbody>
        </table>
        <div class="threadList">
            <%
                boolean pipe = false;
                ThreadInformation thread = null;
                for (ThreadInformation t : failure.getThreadList()) {
                    String keyString = t.keyToString();
                    if (thread == null && (keyString.equals(paramThreadId) || paramThreadId == null)) {
                        thread = t;
                    }
                    if (pipe) {
                        %> | <%
                    } else {
                        pipe = true;
                    }
                    if (t != thread) {
                        %><a href="<%=baseUrl + "&threadId=" + keyString%>"><%=t.getName()%></a><%
                    } else {
                        %><%=t.getName()%><%
                    }
                }
            %>
        </div>
        <a name="loggingEventList"></a>
        <table width="100%">
            <caption>Log</caption>
            <thead>
                <tr>
                    <th>Index</th>
                    <th>Level</th>
                    <th>Logger</th>
                    <th>Message</th>
                    <th>Stack Trace</th>
                </tr>
            </thead>
            <tbody>
            <%
                {
                    List<LoggingEvent> loggingEventList = thread.getLoggingEventList();
                    loggingEventList = loggingEventPageUtil.getResultList(loggingEventList);
                    loggingEventPageUtil.printPagesTr(out, 5);
                    boolean odd = true;
                    for (LoggingEvent le : loggingEventList) {
                        %><tr<%=odd ? " class=\"odd\"" : "" %>>
                            <td><%=le.getIndex()%></td>
                            <td><%=le.getLevel()%></td>
                            <td><%=le.getLoggerName()%></td>
                            <td><%=le.getMessage()%></td>
                            <td><%=le.getSourceLocation().toString()%></td>
                        </tr><%
                        odd = !odd;
                    }
                    loggingEventPageUtil.printPagesTr(out, 5);
                }
            %>
            </tbody>
        </table>
        <br>
        <a name="stackTrace"></a>
        <table width="100%">
            <caption>Stack Trace</caption>
            <thead>
                <tr>
                    <th>Index</th>
                    <th>Stack Trace</th>
                </tr>
            </thead>
            <tbody>
            <%
                {
                    List<StackTraceElement> stackTrace = thread.getStackTrace();
                    stackTrace = stackTracePageUtil.getResultList(stackTrace);
                    stackTracePageUtil.printPagesTr(out, 2);
                    boolean odd = true;
                    for (StackTraceElement st : stackTrace) {
                        %><tr<%=odd ? " class=\"odd\"" : "" %>>
                            <td><%=st.getIndex()%></td>
                            <td><%=st.getSourceLocation().toString()%></td>
                        </tr><%
                        odd = !odd;
                    }
                    stackTracePageUtil.printPagesTr(out, 2);
                }
            %>
            </tbody>
        </table>
    <%
    }
%>
</div>

</body>
</html>